package com.ruoyi.datastatistics.service.impl;

import com.ruoyi.datastatistics.domain.maindata.*;
import com.ruoyi.datastatistics.mapper.MainMapper;
import com.ruoyi.datastatistics.service.IMainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MainServiceImpl implements IMainService {

    @Autowired
    private MainMapper mainMapper;


    @Override
    public Map<String,Object> getMainHeadDataDb(){
        //设备数
        List<MainHeadTerminalsDbData> mainHeadTerminalsDbData = mainMapper.mainHeadTerminalsDbData();
        //用户
        MainHeadUsersDbData mainHeadUsersDbData = mainMapper.mainHeadUsersDbData();
        //在线率
        Integer onlineCount = mainMapper.getOnlineCountDb();
        Integer totalCount = mainMapper.getTotalTerminalsCount();
        BigDecimal bd = new BigDecimal(onlineCount);
        BigDecimal td = new BigDecimal(totalCount);
        BigDecimal onlineRate = td.equals(BigDecimal.ZERO)? BigDecimal.ZERO: bd.divide(td,4,BigDecimal.ROUND_UP);
        MainDayDosageDb  yesterdayDosage = mainMapper.getYesterdayDosage();

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("mainHeadTerminalsDbData",mainHeadTerminalsDbData);//设备数
        map.put("mainHeadUsersDbData",mainHeadUsersDbData);//用户数
        map.put("onlineCount",onlineCount);//在线数
        map.put("totalCount",totalCount);//设备总是
        map.put("onlineRate",onlineRate);//在线率
        map.put("yesterdayDosage",yesterdayDosage);//昨日用量
        return map;
    }

    @Override
    public Map<String,Object> getMainChartsDataDb(String startTime,String endTime,String feezeTime,String getTimeN){
        startTime = startTime + " 00:00:00";
        endTime = endTime + " 23:59:59";
        List<MainChartWarnDataDb> mainChartWarnDataDb  = mainMapper.getMainChartWarnDataDb(startTime,endTime);//今日预警
        List<MainMonthDosageDb> mainMonthDosageDb = mainMapper.getMainMonthDosageDb(feezeTime);//近6个月用量
        List<MainDayDosageDb>  mainDayDosageDb = mainMapper.getMainDayDosageDb(getTimeN);//15天日用量


        Map<String,Object> map = new HashMap<String,Object>();
        map.put("mainChartWarnDataDb",mainChartWarnDataDb);
        map.put("mainMonthDosageDb",mainMonthDosageDb);
        map.put("mainDayDosageDb",mainDayDosageDb);

        //增长率计算
        List<Float> growRate = new ArrayList<Float>();
        if(null!=mainDayDosageDb && mainDayDosageDb.size()>1){
            for(int i = 1;i<mainDayDosageDb.size();i++){//i取0 是为了能和前一条数据做运算，因此要多取一天的数据
                if(null!=mainDayDosageDb.get(i-1).getDosage() && mainDayDosageDb.get(i-1).getDosage()!=0){//null和0判断
                    BigDecimal bd = new BigDecimal((mainDayDosageDb.get(i).getDosage()-mainDayDosageDb.get(i-1).getDosage())/mainDayDosageDb.get(i-1).getDosage());
                    Float onlineRate = bd.multiply(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_UP).floatValue();
                    growRate.add(onlineRate);
                }else{
                    growRate.add(new BigDecimal(100).floatValue());
                }

            }
        }
//        growRate.add(0, (float) 0);
        map.put("growRate",growRate);

        return map;
    }




}
